﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class TransformSyncNet : NetworkBehaviour
{
    public float lerpSpeed;
    
    //服务端数据发生改变，将会同步到客户端，数据从服务器同步到客户端
    [SyncVar] Vector3 serverSidePosition = Vector3.zero;
    [SyncVar] Quaternion serverSideRotation;

    void FixedUpdate()
    {
        if (isLocalPlayer)
        {
            CmdSend2Server(transform.position, transform.rotation);
        }
        else
        {
            LerpPosition();
        }
    }
    
    [Command]
    void CmdSend2Server(Vector3 position, Quaternion rotation)
    {
        serverSidePosition = position;
        serverSideRotation = rotation;
    }

    void LerpPosition()
    {
        transform.position = Vector3.Lerp(transform.position, serverSidePosition, lerpSpeed * Time.fixedDeltaTime);
        transform.rotation = Quaternion.Slerp(transform.rotation, serverSideRotation, lerpSpeed * Time.fixedDeltaTime);
    }
}
